<?php

namespace App\Http\Controllers\CmsApi;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Lib\Response;
use App\Http\Controllers\BaseController;
use App\Http\Model\Hint;
use App\Http\Services\NameplateServices;


class InnerNameplateController extends BaseController
{
    protected $nameplateService;

    public function __construct()
    {
        $this->nameplateService = new NameplateServices();

    }

    /**
     * 铭牌新增
     */
    public function nameplateAdd(Request $request)
    {
        $nameplateName = $request->input('nameplate_name', '');
        $nameplateImg = $request->input('nameplate_img', '');
        $nameplateResource = $request->input('nameplate_resource', '');
        $nameplateDesc = $request->input('nameplate_desc', '');
        if(empty($nameplateName) || empty($nameplateImg) || empty($nameplateResource)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('NAMEPLATE:nameplateAdd', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->nameplateService->nameplateAdd($nameplateName, $nameplateImg, $nameplateResource, $nameplateDesc);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 铭牌编辑
     */
    public function nameplateEdit(Request $request)
    {
        $nameplateId = $request->input('nameplate_id', 0);
        $nameplateName = $request->input('nameplate_name', '');
        $nameplateImg = $request->input('nameplate_img', '');
        $nameplateResource = $request->input('nameplate_resource', '');
        $nameplateDesc = $request->input('nameplate_desc', '');
        if(empty($nameplateId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('NAMEPLATE:nameplateEdit', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->nameplateService->nameplateEdit($nameplateId, $nameplateName, $nameplateImg, $nameplateResource, $nameplateDesc);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 铭牌用户下发记录新增
     */
    public function nameplateUserAdd(Request $request)
    {
        $nameplateId = $request->input('nameplate_id', 0);
        $userId = $request->input('user_id', 0);
        $timeSt = $request->input('time_st', '');
        $timeEt = $request->input('time_et', '');
        $sort = (int) $request->input('sort', 0);
        $adminId = (int) $request->input('admin_id', 0);
        if(empty($nameplateId) || empty($userId) || empty($timeSt) || empty($timeEt) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $timeSt = date("Y-m-d H:i:s", strtotime($timeSt));
        $timeEt = date("Y-m-d H:i:s", strtotime($timeEt));
        //锁
        $ifLock = redis_lock('NAMEPLATE:nameplateUserAdd', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->nameplateService->nameplateUserAdd($nameplateId, $userId, $timeSt, $timeEt, $sort, $adminId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 铭牌用户下发记录编辑
     */
    public function nameplateUserEdit(Request $request)
    {
        $nameplateId = $request->input('nameplate_id', 0);
        $userId = $request->input('user_id', 0);
        $timeSt = $request->input('time_st', '');
        $timeEt = $request->input('time_et', '');
        $sort = (int) $request->input('sort', 0);
        $adminId = (int) $request->input('admin_id', 0);
        if(empty($nameplateId) || empty($userId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $timeSt = empty($timeSt) ? '' : date("Y-m-d H:i:s", strtotime($timeSt));
        $timeEt = empty($timeEt) ? '' : date("Y-m-d H:i:s", strtotime($timeEt));
        //锁
        $ifLock = redis_lock('NAMEPLATE:nameplateUserEdit', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->nameplateService->nameplateUserEdit($nameplateId, $userId, $timeSt, $timeEt, $sort, $adminId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 铭牌用户下发记录删除
     */
    public function nameplateUserDel(Request $request)
    {
        $nameplateId = $request->input('nameplate_id', 0);
        $userId = $request->input('user_id', 0);
        $adminId = (int) $request->input('admin_id', 0);
        if(empty($nameplateId) || empty($userId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('NAMEPLATE:nameplateUserDel', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->nameplateService->nameplateUserDel($nameplateId, $userId, $adminId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 铭牌用户系统下发
     */
    public function nameplateUserSend(Request $request)
    {
        $nameplateId = $request->input('nameplate_id', 0);
        $userId = $request->input('user_id', 0);
        $second = $request->input('second', 0);
        $sort = $request->input('sort', 0);
        if(empty($nameplateId) || empty($userId) || empty($second)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->nameplateService->nameplateUserSend($nameplateId, $userId, $second, $sort);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

}